package com.example.ll_video.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.ll_video.R;


public class CommonDialog {
    private View dialogView;
    private TextView titleView;
    private Dialog dialogBuilder;
    private TextView tvLeft;
    private TextView tvRight;
    private TextView tvMessage;
    private TextView onlyBtn;
    private LinearLayout llBottom2;
    private LinearLayout llBottom;
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        titleView.setText(title);
    }

    public interface DialogClick {
        void selectBtn(String str, int i);
    }

    public CommonDialog(final Context context, String message, final String left, final String right, final DialogClick click
                             ) {
        dialogView = LayoutInflater.from(context).inflate(R.layout.q_common_dialog,
                null);
        dialogBuilder = new Dialog(context, R.style.common_dialog_bg);
        dialogBuilder.setCanceledOnTouchOutside(false);
        dialogBuilder.setCancelable(false);
        Window win = dialogBuilder.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        Window window = dialogBuilder.getWindow();
        window.setGravity(Gravity.CENTER);
        dialogBuilder.setContentView(dialogView);

        tvLeft = (TextView) dialogView.findViewById(R.id.left);
        tvRight = (TextView) dialogView.findViewById(R.id.right);
        tvMessage = (TextView) dialogView.findViewById(R.id.message);
        titleView = (TextView) dialogView.findViewById(R.id.title);
        tvLeft.setText(left);
        tvRight.setText(right);
        tvMessage.setText(message);
        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.left:
                        click.selectBtn(left, 0);
                        closeDialog();
                        break;
                    case R.id.right:
                        click.selectBtn(right, 1);
                        closeDialog();
                        break;

                    default:
                        break;
                }
            }
        };
        tvLeft.setOnClickListener(clickListener);
        tvRight.setOnClickListener(clickListener);


    }



    public CommonDialog(final Context context, int type, String message, final String left, final String right, final DialogClick click
    ) {
        dialogView = LayoutInflater.from(context).inflate(R.layout.q_common_dialog,
                null);
        dialogBuilder = new Dialog(context, R.style.common_dialog_bg);
        dialogBuilder.setCanceledOnTouchOutside(false);
        dialogBuilder.setCancelable(false);
        Window win = dialogBuilder.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        Window window = dialogBuilder.getWindow();
        window.setGravity(Gravity.CENTER);
        dialogBuilder.setContentView(dialogView);

        tvLeft = (TextView) dialogView.findViewById(R.id.left);
        tvRight = (TextView) dialogView.findViewById(R.id.right);
        tvMessage = (TextView) dialogView.findViewById(R.id.message);
        titleView = (TextView) dialogView.findViewById(R.id.title);
        tvLeft.setText(left);
        tvRight.setText(right);

//        tvMessage.setText(message);
        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.left:
                        click.selectBtn(left, 0);
                        closeDialog();
                        break;
                    case R.id.right:
                        click.selectBtn(right, 1);
                        closeDialog();
                        break;

                    default:
                        break;
                }
            }
        };
        tvLeft.setOnClickListener(clickListener);
        tvRight.setOnClickListener(clickListener);


    }

    public CommonDialog(final Context context, String message, final String btnText, final DialogClick click
    ) {
        dialogView = LayoutInflater.from(context).inflate(R.layout.q_common_dialog,
                null);
        dialogBuilder = new Dialog(context, R.style.common_dialog_bg);
        dialogBuilder.setCanceledOnTouchOutside(false);
        dialogBuilder.setCancelable(false);
        Window win = dialogBuilder.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        Window window = dialogBuilder.getWindow();
        window.setGravity(Gravity.CENTER);
        dialogBuilder.setContentView(dialogView);

        tvLeft = (TextView) dialogView.findViewById(R.id.left);
        tvRight = (TextView) dialogView.findViewById(R.id.right);
        tvMessage = (TextView) dialogView.findViewById(R.id.message);
        titleView = (TextView) dialogView.findViewById(R.id.title);
        onlyBtn = (TextView) dialogView.findViewById(R.id.tv_bottom_button);
        llBottom = (LinearLayout) dialogView.findViewById(R.id.ll_bottom);
        llBottom2 = (LinearLayout) dialogView.findViewById(R.id.ll_bottom2);

        llBottom.setVisibility(View.GONE);
        llBottom2.setVisibility(View.VISIBLE);

//        tvMessage.setText(message);
        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.left:
                        click.selectBtn("left", 0);
                        closeDialog();
                        break;
                    case R.id.right:
                        click.selectBtn("right", 1);
                        closeDialog();
                        break;
                    case R.id.tv_bottom_button:
                        click.selectBtn("right", 2);
                        closeDialog();
                        break;

                    default:
                        break;
                }
            }
        };
        onlyBtn.setOnClickListener(clickListener);

    }
    public void closeDialog() {
        if (dialogBuilder != null && dialogBuilder.isShowing()) {
            TranslateAnimation mInnerDismissAnim = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
                    0, Animation.RELATIVE_TO_SELF, 0,
                    Animation.RELATIVE_TO_SELF, 1);
            // 设置动画时间
            mInnerDismissAnim.setDuration(300);
            dialogView.startAnimation(mInnerDismissAnim);
            mInnerDismissAnim
                    .setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if (dialogBuilder != null) {
                                dialogBuilder.dismiss();
                            }
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });
        }
    }

    public void showDialog() {
        if (dialogBuilder != null && !dialogBuilder.isShowing()) {
            dialogBuilder.show();
            // 初始化
            TranslateAnimation mInnerShowAnim = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
                    0, Animation.RELATIVE_TO_SELF, 1f,
                    Animation.RELATIVE_TO_SELF, 0);
            // 设置动画时间
            mInnerShowAnim.setDuration(300);
            dialogView.startAnimation(mInnerShowAnim);

        }
    }

    public boolean isShowingDialogs() {
        return dialogBuilder.isShowing();
    }

    public void recycleClearDialog() {
        if (dialogBuilder != null) {
            if (dialogBuilder.isShowing()) {
                dialogBuilder.dismiss();
            }
            dialogBuilder = null;
        }
    }

    public void setCancel(boolean flag) {
        if (dialogBuilder != null) {
            dialogBuilder.setCancelable(flag);
        }
    }

}
